﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iMapa.UI.ModelView.Main;
using iMapa.UI.View.Main;
using iMapa.UI.View.Main.Wizard;
using iMapa.UI.View.Simulation;
using System.Collections.ObjectModel;
using iMapa.UI.ModelView.Main.Wizard;
using iMapa.Backend;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace iMapa
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);


        public MainWindow()
        {

            InitializeComponent();
            
        }



        private void exit_Click(object sender, RoutedEventArgs e)
        {
            MainViewModel modelView = (MainViewModel)this.DataContext;
            if (modelView.CurrentViewObject is MapViewModel)
            {
                MapViewModel model = (MapViewModel)modelView.CurrentViewObject;
                InteractMapLoader.Save(model.Imap);
                modelView.RestoreMain();
            }
            else if (modelView.CurrentViewObject is GroupViewModel)
            {
                SimulationController controller = SimulationController.getInstance();
                if (controller.SimulationRunning)
                {
                    controller.StopSimulation();
                    while (!controller.SimulationStopped)
                    {
                        Thread.Sleep(10);
                    }
                }
                modelView.RestoreMain();
            }
            else
            {
                string messageBoxText = "Would you like to save the current state of interaction maps?";
                string caption = "Save changes?";
                MessageBoxButton button = MessageBoxButton.YesNoCancel;
                MessageBoxImage icon = MessageBoxImage.Question;
                MessageBoxResult messageBoxResult = MessageBox.Show(messageBoxText, caption, button, icon);
                
                switch (messageBoxResult)
                {
                    case MessageBoxResult.Yes:
                        InteractMapLoader.saveOnExit=true;
                        SaveImaps();
                        this.Close();
                        break;
                    case MessageBoxResult.No:
                        InteractMapLoader.saveOnExit = false;
                        DeleteTempDir();
                        this.Close();
                        break;
                    case MessageBoxResult.Cancel: 
                        break;
                }
            }
        }

        private void SaveImaps(){
            if (InteractMapLoader.saveOnExit)
            {
                ProgressIndicatorViewModel model = new ProgressIndicatorViewModel();
                Window w = new SavingProgressBarWindow();
                w.DataContext = model;
                w.Owner = Window.GetWindow(this);
                Thread MyThread = new Thread(InteractMapLoader.PackAndRemoveTemp);
                MyThread.Start(model);
                w.ShowDialog();
            }
        }

        private void DeleteTempDir()
        {
            if (!InteractMapLoader.saveOnExit)
            {
                try
                {
                    Directory.Delete(InteractMapLoader.temppath, true);
                }
                catch (Exception e) { }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
            
            
        }
    }
}
